<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 19:26
 */

namespace app\admin\controller;


use app\admin\service\ConfigGroupService;
use app\admin\service\ConfigService;
use app\Request;
use think\App;
use think\Exception;
use think\model\Relation;

class Config extends AdminBase
{
    protected $configService = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->configService = new ConfigService();
    }

    public function index()
    {
        // 获取配置组信息
        try {
            $config_groups = \app\common\model\mysql\ConfigGroup::with(
                [
                    'configs' => function (Relation $query) {
                        $query->field(
                            'config_id,config_name,config_title,config_value,config_type,config_desc,group_id,config_info'
                        );
                    },
                ]
            )->select()->toArray();
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        return view('config/index', compact('config_groups'));
    }

    public function create()
    {
        try {
            $config_groups = (new ConfigGroupService())->getAllGroups();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        return view('config/create', compact('config_groups'));
    }

    public function save(Request $request)
    {
        if (!$request->isAjax() || !$request->isPost()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        $data = [
            'config_type'  => $request->post('config_type', "", "trim"),
            'group_id'     => $request->post('group_id', 0, 'intval'),
            'config_name'  => $request->post('config_name', "", "trim"),
            'config_title' => $request->post('config_title', "", "trim"),
            'config_value' => $request->post('config_value', "", "trim"),
            'config_info'  => $request->post('config_info', "", "trim"),
            'config_desc'  => $request->post('config_desc', "", "trim"),
        ];

        // 验证数据
        $result = $this->validate($data, "ConfigValidate.add");
        if (true !== $result) {
            return writeJson(config('status.error'), $result);
        }

        try {
            $res = $this->configService->addConfig($data);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), "配置添加成功");
        }

        return writeJson(config('status.error'), "配置添加失败");
    }

    /**
     * 系统配置
     * @param Request $request
     */
    public function webConfig(Request $request)
    {
        if ($request->isPost()) {
            $data = $_POST['config'];
            if (!is_array($data)) {
                $this->error('数据不能为空');
            }
            foreach ($data as $k => $v) {
                try {
                    $res = $this->configService->saveConfig($k, $v);
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
            }

            if ($res) {
                $this->success('修改成功', url('Config/index'));
            }
            $this->error('修改失败');
        }
    }

    public function edit($config_id)
    {
        try {
            $config_info = $this->configService->getOneConfigByConfigId($config_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($config_info) {
            return view('config/edit', compact('config_info'));
        }

        $this->error('该配置不存在');
    }

    public function update(Request $request)
    {

    }

    public function delete(Request $request)
    {

    }
}